package com.college.heyuhua.service.impl;

import com.college.heyuhua.dao.ShopMapper;
import com.college.heyuhua.model.shop.*;
import com.college.heyuhua.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵东坡 on 2019/2/6.
 * Time:12:53
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Service
public class ShopServiceImpl implements IShopService {

    @Autowired
    private ShopMapper shopMapper;


    @Override
    public List<Item> getItemList() {
        return shopMapper.getItemList();
    }

    @Override
    public List<Category> getCategoryList() {
        return shopMapper.getCategoryList();
    }

    @Override
    public List<Item> getItemListByCid(Integer cid) {
        return shopMapper.getItemListByCid(cid);
    }

    @Override
    public boolean addcart(Integer id, int fck,long uid) {

        //先查询是否商品数量够
        int counts  = shopMapper.getCounts(id);
        if(counts > 0) {

            /*查看购物车中是否有同样的商品*/
            Cart cart = shopMapper.getItemByItemid(id+"");
            int a ;
            if(cart != null) {
                a = shopMapper.updatecartcount(id,fck,uid,cart.getCount()+1);
            }else{
                a = shopMapper.addcart(id,fck,uid);
            }
            return a>0?true:false;
        }
        return false;
    }

    @Override
    public List<Item> getCart(long uid) {

        List<Cart> cartList = shopMapper.getCart(uid);

        List<Item> itemsList = new ArrayList<>();
        for(Cart cart:cartList) {
            Item item = shopMapper.getItemFromCart(cart);
            itemsList.add(item);
        }
        return itemsList;
    }

    @Override
    public boolean updates(String iid,String counts) {
        return shopMapper.updates(iid,counts)>0?true:false;
    }

    @Override
    public boolean deletes(String iid) {
        return shopMapper.deletes(iid)>0?true:false;
    }

    @Override
    public boolean shoptheall(long uid) {
        List<Cart> cartList = shopMapper.getCart(uid);
        List<String> itemList = new ArrayList<>();
        for(Cart cart:cartList) {
            shopMapper.deletes(cart.getItemid());
            itemList.add(cart.getItemid());
        }
        return shopMapper.insertOrder(itemList.toString(),getRandom(),uid)>0?true:false;
    }

    @Override
    public int getCartInfo(long iid) {
        return shopMapper.getCartInfo(iid);
    }

    @Override
    public Item getItemByIid(String iid) {
        return shopMapper.getItemByIid(iid);
    }

    @Override
    public boolean addcartbycounts(int iid, int fck, long uid, String countss) {
        return shopMapper.addcartbycounts(iid,fck,uid,countss);
    }

    @Override
    public List<Orderinfo> getOrderByuid(long uid) {
        return shopMapper.getOrderByuid(uid);
    }

    @Override
    public Take_delivery_of_goods getTakeofgoods(long uid) {
        return shopMapper.getTakeofgoods(uid);
    }

    @Override
    public boolean updateinfo(String address, String tel,long uid) {
        return shopMapper.updateinfo(address,tel,uid);
    }

    @Override
    public boolean deletesCart(long uid) {
        return shopMapper.deletesCart(uid);
    }

    @Override
    public boolean addCategory(String cname) {
        return shopMapper.addCategory(cname);
    }

    @Override
    public boolean deletecategory(String cid) {
        return shopMapper.deletecategory(cid);
    }

    @Override
    public boolean addItem(Item item) {
        return shopMapper.addItem(item);
    }


    public static String getRandom() {
        byte[] lock = new byte[0];
        long w = 100000000;
        long r = 0;
        synchronized (lock) {
            r = (long) ((Math.random() + 1) * w);
        }
        return System.currentTimeMillis() + String.valueOf(r).substring(1);
    }


}
